package com.example.maybeaccount;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.maybeaccount.Entry.Entrance;
import com.example.maybeaccount.Entry.User;
import com.example.maybeaccount.Mode.ItemAdapter;
import com.example.maybeaccount.Mode.SettingAdapter;
import com.example.maybeaccount.Mode.SpacesItemDecoration;
import com.example.maybeaccount.Mode.SqliteDB;

import java.util.ArrayList;

/** 设置活动 */
public class ActivitySetting extends AppCompatActivity {
    private RecyclerView recyclerView ;
    private ImageButton buttonBack;
    private int firstFlag = 0;
    private boolean isTop;
    private AlertDialog alertDialog2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBroadcast();
        setContentView(R.layout.activity_setting);

        recyclerView = findViewById(R.id.recycler_view);
        buttonBack = findViewById(R.id.button_back);

        buttonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        setRecyclerView();
    }

    private void setRecyclerView(){

        LinearLayoutManager layoutManager= new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //设置各个item之间的间隔
        int space = 2;
        recyclerView.addItemDecoration(new SpacesItemDecoration(space));
        SettingAdapter settingAdapter = new SettingAdapter(this);
        recyclerView.setAdapter(settingAdapter);

    }

    /**
     * 后台唤醒后进入密码界面
     */
    @Override
    protected void onResume(){
        super.onResume();
        isTop = true;
        if(firstFlag == 1){
            firstFlag = 0;
            Intent intent = new Intent(ActivitySetting.this, Entrance.class);
            intent.putExtra("resume","Setting");
            startActivity(intent);
        }else{
            firstFlag = 0;
        }
    }

    @Override
    protected void onPause(){
        super.onPause();
        isTop = false;
    }

    /** 单选弹窗 */
    public void showSingleAlertDialog( ){
        final String[] items = {"全部","文本密码", "图形密码", "生物识别"};
        final String[] chooseMethod = {"全部"};
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
        alertBuilder.setTitle("请选择默认登录方式");
        alertBuilder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                chooseMethod[0] = items[i];
            }
        });
        alertBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                User user = SqliteDB.getInstance(getApplicationContext()).loadUser().get(0);
                switch (chooseMethod[0]){
                    case "文本密码":
                        user.setDefaultUnlockMethod(User.UNLOCK_METHOD_TEXT);
                        break;
                    case "图形密码":
                        if(user.getGridPwd().equals("00000")){
                            Toast.makeText(ActivitySetting.this,"您还未设置图形密码",Toast.LENGTH_LONG).show();
                            return;
                        }
                        user.setDefaultUnlockMethod(User.UNLOCK_METHOD_GRID);
                        break;
                    case "生物识别":
                        user.setDefaultUnlockMethod(User.UNLOCK_METHOD_BIO);
                        break;
                    case "全部":
                        user.setDefaultUnlockMethod(User.UNLOCK_METHOD_ALL);
                        break;
                    default:
                }
                if(SqliteDB.getInstance(getApplicationContext()).updateUserUnlockMethod(user) == 1){
                    Toast.makeText(ActivitySetting.this,"设置成功",Toast.LENGTH_LONG).show();

                }
                alertDialog2.dismiss();
            }
        });
        alertBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                alertDialog2.dismiss();
            }
        });
        alertDialog2 = alertBuilder.create();
        alertDialog2.show();
    }

    /** 广播监听home和menu */
    class InnerRecevier extends BroadcastReceiver {
        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if (reason != null) {
                    if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY) && isTop) {
                        // "Home键被监听"
                        firstFlag = 1;

                    } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS) && isTop) {
                        //"多任务键被监听"
                        firstFlag = 1;
                    }
                }
            }
        }
    }

    private void initBroadcast(){
        /** 广播 */
        this.getWindow().setFlags(0x80000000,0x80000000);
        //创建广播
        InnerRecevier innerReceiver = new InnerRecevier();
        //动态注册广播
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        //启动广播
        registerReceiver(innerReceiver, intentFilter);
    }
}
